package com.dhh.dao;

import com.dhh.vo.Goal;

import android.content.ContentValues;
import android.database.Cursor;

public class GoalDAO {
	private DataBaseHelper db;
	
	public GoalDAO(DataBaseHelper db){
		this.db = db;
	}
	
	public Goal getLastUpdateGoal(){
		try{
			String whereSQL = "_id=(select max(_id) from goals)";
			String[] columns = Goal.getColumns();
			Cursor cursor = db.getReadableDatabase().query(true, "goals", columns, whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				Goal goal = new Goal();
				goal.setId(cursor.getString(0));
				goal.setTargetWeight(cursor.getString(1));
				goal.setWayGain(cursor.getString(2));
				goal.setGender(cursor.getString(3));
				goal.setAge(cursor.getString(4));
				goal.setHeight(cursor.getString(5));
				goal.setWeight(cursor.getString(6));
				goal.setActivityLevel(cursor.getString(7));
				cursor.close();
				return goal;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			//if(db!=null) db.close();
		}
		
		return new Goal();
	}
	
	public boolean addGoal(Goal goal){
		boolean flag = false;
		ContentValues initialValues = createContentValues(goal);
		try{
			db.openDataBase();
			flag = db.getWritableDatabase().insert("goals", null, initialValues)>-1;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			//if(db!=null) db.close();
		}
		return flag;
	}
	
	private ContentValues createContentValues(Goal goal){
		ContentValues values = new ContentValues();
		values.put(Goal.C_TARGET_WEIGHT, goal.getTargetWeight());
		values.put(Goal.C_WAY_GAIN, goal.getWayGain());
		values.put(Goal.C_GENDER, goal.getGender());
		values.put(Goal.C_AGE, goal.getAge());
		values.put(Goal.C_HEIGHT, goal.getHeight());
		values.put(Goal.C_WEIGHT, goal.getWeight());
		values.put(Goal.C_ACTIVITY_LEVEL, goal.getActivityLevel());
		return values;
	}
}
